//
//  LTNewsController.m
//  wangyiNews08
//
//  Created by 123 on 16/3/11.
//  Copyright © 2016年 Tsr. All rights reserved.
//

#import "LTNewsController.h"
#import "LTNews.h"
#import "LTNewsCell.h"

@interface LTNewsController ()

@property (nonatomic, strong) NSArray *newsList;

@end

@implementation LTNewsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 在iOS7.0之后，如果项目中有导航控制器存在，则scrollView的默认的contentInset = (64,0,0,0)
    self.automaticallyAdjustsScrollViewInsets = NO;
    
//    [self loadData];
}

- (void)setURLString:(NSString *)URLString {
    _URLString = URLString;
    
    [self loadData];
   
}

// 加载数据
- (void) loadData {
    [LTNews newsWithURLString:self.URLString success:^(NSArray *news) {
        
        self.newsList = news;
        
        [self.tableView reloadData];
    } error:^(NSError *er) {
        
    }];
}

#pragma mark - Table view data source
// 返回多少个item
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.newsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 获得模型
    LTNews *news = self.newsList[indexPath.row];
    
    LTNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:[LTNewsCell cellIdentifier:news]];
    
    cell.news = news;
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LTNews *news = self.newsList[indexPath.row];
    
    return [LTNewsCell cellHeight:news];
}
@end
